import React from "react";
import {ModalForm, ProFormText} from "@ant-design/pro-form/es";
import { Space} from "antd";
import {ProFormDateTimePicker, ProFormDigit, ProFormSelect} from "@ant-design/pro-form";
import {fetchAllDriver} from "@/services/Driver";
import {fetchAllStation} from "@/services/Station";
import {fetchAllBus} from "@/services/Bus";

const BusNoInfoForm: React.FC = (props: any) => {

  const fetchDriverOption = async () => {
    const res = await fetchAllDriver({
      current: 0, pageSize: -1
    }, null, null);
    return res.data.map((value: Domain.Driver)=>{
      return {
        label: value.name,
        value: value.account
      }
    });
  }

  const fetchStationOption = async () => {
    const res = await fetchAllStation({
      current: 0, pageSize: -1
    }, null, null);
    return res.data.map((value: Domain.Station)=>{
      return {
        label: value.name,
        value: value.id
      }
    });
  }

  const fetchBusOption = async () => {
    const res = await fetchAllBus({
      current: 0, pageSize: -1
    }, null, null);
    return res.data.map((value: Domain.Bus)=>{
      return {
        label: value.busNum,
        value: value.busNum
      }
    });
  }

  return (
    <ModalForm<Domain.BusNo>
      title={props.title}
      trigger={props.trigger}
      autoFocusFirstInput
      modalProps={{
        onCancel: () => console.log('run'),
      }}
      onFinish={async (values) => {
        if (props.handleAdd)
          return await props.handleAdd(values)
        if (props.handleUpdate) {
          //@ts-ignore
          values.fromTime = new Date(Date.parse(values.fromTime));
          //@ts-ignore
          values.toTime = new Date(Date.parse(values.toTime));
          return await props.handleUpdate(values)
        }
      }}
      layout="horizontal"
      labelCol={{span: 7}}
      wrapperCol={{span: 14}}
      initialValues={props.busNo}
    >

      <Space direction={"vertical"} size={16}>{
        props.busNo &&
        <ProFormText
          width="md"
          name="id"
          label="车次ID"
          disabled
        />
      }

        <ProFormDigit
          width="md"
          name="price"
          label="价格"
          placeholder="请输入价格"
          required
        />

        <ProFormSelect
          request={fetchDriverOption}
          name="driverId"
          label="司机"
          placeholder="请选择司机"
          required
        />

        <ProFormSelect
          request={fetchBusOption}
          name="busNum"
          label="汽车"
          placeholder="请选择汽车"
          required
        />

        <ProFormSelect
          request={fetchStationOption}
          name="startStation"
          label="起始车站"
          placeholder="请选择起始车站"
          required
        />

        <ProFormSelect
          request={fetchStationOption}
          name="endStation"
          label="终点车站"
          placeholder="请选择终点车站"
          required
        />

        <ProFormDateTimePicker
          name="fromTime"
          label="发车时间"
          placeholder="请选择发车时间"
          required
          />

        <ProFormDateTimePicker
          name="toTime"
          label="到达时间"
          placeholder="请选择到达时间"
          required
          />

      </Space>
    </ModalForm>
  );
}

export default BusNoInfoForm;
